﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Zero.UControl.Demo.Views
{
    /// <summary>
    /// USwitchDemo.xaml 的交互逻辑
    /// </summary>
    public partial class USwitchDemo : UserControl
    {
        private double _currentLeft = 0;
        private double _currentWidth = 0;

        public USwitchDemo()
        {
            InitializeComponent();
            Loaded += (s, e) =>
            {
                var firstButton = (RadioButton)ButtonPanel.Children[0];
                firstButton.IsChecked = true;
            };
        }

        /// <summary>
        /// 整体处理思路分析：
        ///     以 RootGrid 作为父容器，剩下元素内的坐标都是相对父容器二设置的
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {
            var button = (FrameworkElement)sender;

            // 获取按钮左上角在 RootGrid 中的坐标
            Point buttonPos = button.TransformToAncestor(RootGrid).Transform(new Point(0, 0));
            double buttonLeft = buttonPos.X;
            double buttonTop = buttonPos.Y;
            double buttonBottom = buttonTop + button.ActualHeight;
            double buttonWidth = button.ActualWidth;

            // 初始化（第一次）
            if (_currentWidth == 0)
            {
                UnderlineTranslate.X = buttonLeft;
                Underline.Width = buttonWidth;
                _currentLeft = buttonLeft;
                _currentWidth = buttonWidth;
                return;
            }

            // 设置下划线的 Canvas.Top（与按钮底部对齐 + 可加间距）
            Canvas.SetTop(Underline, buttonBottom + 2); // 2 是按钮与下划线之间的间距

            if (buttonLeft > _currentLeft)
                AnimateFixedLeft(buttonLeft, buttonWidth);
            else
                AnimateFixedRight(buttonLeft, buttonWidth);
        }


        /// <summary>
        /// 固定左边，右边扩展再收缩
        /// </summary>
        private void AnimateFixedLeft(double targetLeft, double targetWidth)
        {
            double targetRight = targetLeft + targetWidth;
            double targetExtendWidth = targetRight - _currentLeft;
            double scale = targetExtendWidth / _currentWidth;

            // 第一阶段：固定左边扩展右边
            Underline.RenderTransformOrigin = new Point(0, 0.5);
            UnderlineTranslate.X = _currentLeft;

            var expand = new DoubleAnimation
            {
                To = scale,
                Duration = TimeSpan.FromMilliseconds(300),
                EasingFunction = new ExponentialEase { EasingMode = EasingMode.EaseOut, Exponent = 4 }
            };

            expand.Completed += (s, e) =>
            {
                // 第二阶段：固定右边收缩左边
                _currentLeft = targetLeft;
                _currentWidth = targetWidth;

                Underline.RenderTransformOrigin = new Point(1, 0.5);

                // 更新 Translate.X 以确保右边对齐目标右边
                UnderlineTranslate.X = targetLeft;

                var shrink = new DoubleAnimation
                {
                    To = 1.0,
                    Duration = TimeSpan.FromMilliseconds(300),
                    EasingFunction = new ExponentialEase { EasingMode = EasingMode.EaseIn, Exponent = 4 }
                };

                UnderlineScale.BeginAnimation(ScaleTransform.ScaleXProperty, shrink);
            };

            UnderlineScale.BeginAnimation(ScaleTransform.ScaleXProperty, expand);
        }



        /// <summary>
        /// 固定右边，左边扩展再收缩
        /// </summary>
        private void AnimateFixedRight(double targetLeft, double targetWidth)
        {
            double currentRight = _currentLeft + _currentWidth;
            double extendWidth = currentRight - targetLeft;
            double scale = extendWidth / _currentWidth;

            // 第一阶段：固定右边，左侧扩展
            Underline.RenderTransformOrigin = new Point(1, 0.5);
            UnderlineTranslate.X = currentRight - _currentWidth;

            var expand = new DoubleAnimation
            {
                To = scale,
                Duration = TimeSpan.FromMilliseconds(300),
                EasingFunction = new ExponentialEase { EasingMode = EasingMode.EaseOut, Exponent = 4 }
            };

            expand.Completed += (s, e) =>
            {
                // 第二阶段：固定左边，右侧收缩
                _currentLeft = targetLeft;
                _currentWidth = targetWidth;

                Underline.RenderTransformOrigin = new Point(0, 0.5);
                UnderlineTranslate.X = _currentLeft;

                var shrink = new DoubleAnimation
                {
                    To = 1.0,
                    Duration = TimeSpan.FromMilliseconds(300),
                    EasingFunction = new ExponentialEase { EasingMode = EasingMode.EaseIn, Exponent = 4 }
                };

                UnderlineScale.BeginAnimation(ScaleTransform.ScaleXProperty, shrink);
            };

            UnderlineScale.BeginAnimation(ScaleTransform.ScaleXProperty, expand);
        }

    }

}
